﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// 同步位置，去掉了官方的自带组件networkTransfrom
/// </summary>
public class NetWorkSync : NetworkBehaviour
{
    [SerializeField]
    public float lerpSpeed; 
    [SyncVar]
    Vector3 serverSidePos = Vector3.zero;
    [SyncVar]
    Quaternion serverSideRotation = Quaternion.identity;

    void FixedUpdate()
    {
        if (isLocalPlayer)
        {
            CmdSend2Server(transform.position, transform.rotation);
        }
        else
        {
            LerpPosition();
        }
    }

    void LerpPosition()
    {
        transform.position = Vector3.Lerp(transform.position, serverSidePos, lerpSpeed * Time.deltaTime);
        transform.rotation = Quaternion.Slerp(transform.rotation, serverSideRotation, lerpSpeed * Time.deltaTime);
    }

    [Command]
    void CmdSend2Server(Vector3 position, Quaternion rotation)
    {
        serverSidePos = position;
        serverSideRotation = rotation;
    }
}
